<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客编辑页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/edit.css">
    <link rel="stylesheet" href="blog-editormd/css/editormd.css" />

</head>

<body>
    <div class="nav">
        <img src="pic/logo2.jpg" alt="">
        <span class="blog-title">小粮博客</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
        <a class="nav-span" href="#" onclick="logout()">注销</a>
    </div>
    <!-- style="height: 38px;width: 828px;" -->
    <div class="content-edit">
        <select id="tech-select" class="tech-dropdown">
            <option value="" disabled selected>请选择你要发表的文章类型~</option>
        </select>
        <div class="push">
            <input type="text" name="" id="title">
            <input type="button" value="发布文章" id="submit" onclick="submit()">
        </div>
        <!-- markdown 插件 html代码 -->
        <div id="editor">
            <textarea style="display:none;" id="content" name="content" onclick="editClick">##在这里写下一篇博客</textarea>
        </div>
    </div>

    <script src="js/jquery.min.js"></script>
    <script src="blog-editormd/editormd.min.js"></script>
    <script src="js/common.js"></script>
    <script type="text/javascript">

        $(function () {
            var editor = editormd("editor", {
                width: "100%",
                height: "550px",
                path: "blog-editormd/lib/"
            });

        });

        // var url = "/user/getAuthorInfo" 
        //     getUserInfo(url);




        loadTechOptions()
        function loadTechOptions() {
            const $select = $('#tech-select');
            $.ajax({
                type: "get",
                url: "/option/getOptions",
                contentType: "application/json",
                success: function (result) {
                    console.log("result.data：", result.data);
                    // $select.find('option:not(:first)').remove();
                    if (result.code == 200 && result.data.length > 0) {
                        $.each(result.data, function (index, item) {
                            $('<option>', {
                                value: item.id,  // 使用id作为value
                                text: item.optionLabel // 使用label作为显示文本
                            }).appendTo($select);
                        });
                    } else {
                        console.log("没有数据???");
                    }
                },
                error: function (error) {
                    if (error.status == 401) {
                        alert("jjjj");
                    }
                }

            });
        }






        function editClick() {
            console.log("清空")
            $('#content').html("");
        }

        function submit() {
            const title = $("#title").val();
            const content = $("#content").val().trim().replace(/#/g, '');
            const kind = $("#tech-select option:selected").text();



            // 校验三者是否有一个为空
            if (!title || !content || !kind) {
                alert("标题、内容或类型不能为空！");
                return; // 阻止提交
            }


            $.ajax({
                type: "post",
                url: "/blog/add",
                // contentType: "application/json",
                // data: JSON.stringify({ title, content, kind }),
                data: {
                    "title" :title,
                    "content": content,
                    "kind": kind
                },



                // data: JSON.stringify({
                //     "title": $("#title").val(),
                //     "content": $("#content").val().trim().replace(/#/g, ''),

                //     // "kind":$("#tech-select").text()
                //     // "kind":$("#tech-select").val()
                //     "kind": $("#tech-select option:selected").text()
                // }),
                success: function (result) {
                    console.log(" result.data:发布", result.data)
                    if (result.code == 200 ) {
                        //发布成功
                        alert("发布成功");
                        location.href = "blog_list.html";
                    } else {
                        if(result.code == -5) {
                            alert("内容审核不通过!");
                        }
                        else{
                            alert("发布失败");
                        }
                    }
                },
                error: function (error) {
                    if (error.status == 401) {
                        location.href = "blog_login.html";
                    }
                }
            });
        }
    </script>
</body>

</html>